package com.oracle.application.config;

import com.oracle.application.utils.RSAUtils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
@Component
@ConfigurationProperties(prefix = "jwt")
public class JwtConfig {
    private String publicKey;
    private String privateKey;

    public String createToken(String name,String loginpwd){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("pwd",loginpwd);

        Date expirDate = DateUtils.addHours(new Date(),24);

        String token = Jwts.builder().setClaims(map).setExpiration(expirDate)
                .setIssuer("system-auth-server").setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.RS256, RSAUtils.privateKeyStrToPrivate(privateKey)).compact();

        return token;
    }
}
